package com.greenantmall.adapter;

import java.text.DecimalFormat;
import java.util.List;

import com.avos.avoscloud.AVUser;
import com.greenantmall.R;
import com.greenantmall.AntHomeApplication;
import com.greenantmall.activity.DetailActivity;
import com.greenantmall.bean.Goods;
import com.greenantmall.bean.OrderItem;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.view.CommonDialog;
import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class OrderDetailListAdapter extends BaseAdapter
{

    private List<OrderItem> list;
    private Context context;
    //BitmapUtils bitmapUtils;
    DecimalFormat df = new DecimalFormat("#########.##");
    public OrderDetailListAdapter(List<OrderItem> orderItemList, Context ctx)
    {
        this.list = orderItemList;
        this.context = ctx;
//        bitmapUtils = new BitmapUtils(ctx,ctx.getCacheDir().getAbsolutePath());
//        bitmapUtils.configDiskCacheEnabled(true);
//        bitmapUtils.configMemoryCacheEnabled(true);
//        bitmapUtils.configDefaultLoadingImage(R.drawable.pictures_no);
//        bitmapUtils.configThreadPoolSize(3);
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {

        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = inflater.inflate(R.layout.orderdetailitem, null);
            viewHolder = new ViewHolder();

            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
            viewHolder.price = (TextView) convertView.findViewById(R.id.iv_googds_price);
            viewHolder.cost = (TextView) convertView.findViewById(R.id.iv_googds_discount);
            viewHolder.quantity = (TextView) convertView.findViewById(R.id.iv_googds_quantity);
            viewHolder.weight = (TextView) convertView.findViewById(R.id.iv_googds_weight);
            
            convertView.setTag(viewHolder);

        } else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final OrderItem orderItem = list.get(position);
       
        //orderItem.getName();
        viewHolder.name.setText(orderItem.getName());
        viewHolder.name.setSelected(true);
        
        ImageLoader.getInstance().displayImage(orderItem.getPicUrl(), viewHolder.icon,AntHomeApplication.imageDisplayOptions);
       
        //单价
        viewHolder.price.setText( context.getString(R.string.orderdetail_price, String.valueOf(orderItem.getPrice())+"/"+orderItem.getUnit()));
        
        if(orderItem.isWeighAble())
        {
            viewHolder.weight.setVisibility(View.VISIBLE);
            if(orderItem.getWeight()<0.000001)
            {
                viewHolder.weight.setText(context.getString(R.string.orderdetail_weight, "--.--"+orderItem.getUnit()));
                viewHolder.cost.setText(context.getString(R.string.orderdetail_cost, "--.--") );
            }
            else{
                viewHolder.weight.setText(context.getString(R.string.orderdetail_weight, String.valueOf(orderItem.getWeight()+orderItem.getUnit())));  
                viewHolder.cost.setText(context.getString(R.string.orderdetail_cost, String.valueOf(df.format(orderItem.getWeight()*orderItem.getPrice()))));
            }
            //数量
            viewHolder.quantity.setText( context.getString(R.string.orderdetail_quantity, String.valueOf(orderItem.getQuantity())+orderItem.getWeighUnit()));
        }
        else{
            
            viewHolder.weight.setVisibility(View.INVISIBLE);  
            viewHolder.cost.setText(context.getString(R.string.orderdetail_cost, String.valueOf(df.format(orderItem.getQuantity()*orderItem.getPrice()))));
       
            //数量
            viewHolder.quantity.setText( context.getString(R.string.orderdetail_quantity, String.valueOf(orderItem.getQuantity())+orderItem.getUnit()));
        }
        
        return convertView;
    }

    class ViewHolder
    {

        public TextView name;
        public ImageView icon;
        public TextView price;
        public TextView cost;
        public TextView  quantity;
        public TextView  weight;

    }

}
